#include "main.h"
#include "gpio.h"
#include "systick.h"
void LED_init(void);
void LED_TOGGLE(void);
void LED_LEVEL(u8 bool);
void led_task_init(void);

Timer_t sotftime_led;

void LED_init(void)
{
  { /* Enable peripheral clock                                                                              */
    CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
    CKCUClock.Bit.AFIO = 1;
    CKCUClock.Bit.PB = 1;
    CKCU_PeripClockConfig(CKCUClock, ENABLE);
  }

  { /* Configure GPIO as output mode                                                                        */

    /* Configure AFIO mode as GPIO                                                                          */
    AFIO_GPxConfig(LED_AF_PORT, LED_AF_PIN, AFIO_FUN_GPIO);

    /* Avoid input floating                                                                                 */
    /* Configure GPIO pull resistor                                                                         */
    GPIO_PullResistorConfig(LED_PORT, LED_PIN, GPIO_PR_UP);

    /* Default value RESET/SET                                                                              */
    GPIO_WriteOutBits(LED_PORT, LED_PIN, RESET);

    /* Configure GPIO direction as output                                                                   */
    GPIO_DirectionConfig(LED_PORT, LED_PIN, GPIO_DIR_OUT);
    GPIO_DriveConfig(LED_PORT, LED_PIN,GPIO_DV_8MA);        
  }
  led_task_init();//初始化软件定时器
}

void LED_TOGGLE(void)
{
  static u8 flag = true;
  if(flag)
  {
    GPIO_SetOutBits(LED_PORT, LED_PIN);
    flag = !flag;
  }
  else
  {
    GPIO_ClearOutBits(LED_PORT, LED_PIN);
    flag = !flag;
  }
}

void LED_LEVEL(u8 bool)
{
  if(bool)
  {
      GPIO_SetOutBits(LED_PORT, LED_PIN);
  }
  else
  {
      GPIO_ClearOutBits(LED_PORT, LED_PIN);
  }
}

static void led_proc(void *arg)
{
	// LED_TOGGLE();
	printf("tim:%d\n",tick_get());
}

void led_task_init(void)
{
  TimerInit(&sotftime_led,1000 - 1,TIMER_MODE_PERIODIC,led_proc,NULL);
  TimerStart(&sotftime_led);
}
